<?php

// +----------------------------------------------------------------------
// | 框架公共入口文件
// +----------------------------------------------------------------------
//框架需要运行于PHP5.0版本之上
if (version_compare(PHP_VERSION, '5.0.0', '<'))
	die('require PHP > 5.0');
if (version_compare(PHP_VERSION, '6.0.0', '<')) {
	@set_magic_quotes_runtime(0);
	define('MAGIC_QUOTES_GPC', get_magic_quotes_gpc() ? true : false);
}
//记录开始运行时间
$GLOBALS['_beginTime'] = microtime(TRUE);

// 记录内存初始使用
define('MEMORY_LIMIT_ON', function_exists('memory_get_usage'));
if (MEMORY_LIMIT_ON)
	$GLOBALS['_startUseMems'] = memory_get_usage();

//获取配置值
function C($name=null, $value=null) {
	static $_config = array();
	if (empty($name))
		return $_config;

	// 优先执行设置获取或赋值
	if (is_string($name)) {
		if (!strpos($name, '.')) {
			$name = strtolower($name);
			if (is_null($value))
				return isset($_config[$name]) ? $_config[$name] : null;
			$_config[$name] = $value;
			return;
		}
		// 二维数组设置和获取支持
		$name = explode('.', $name);
		$name[0] = strtolower($name[0]);
		if (is_null($value))
			return isset($_config[$name[0]][$name[1]]) ? $_config[$name[0]][$name[1]] : null;
		$_config[$name[0]][$name[1]] = $value;
		return;
	}
	// 批量设置
	if (is_array($name))
		return $_config = array_merge($_config, array_change_key_case($name));
	return null;
}

// 系统目录定义
if (!defined('FEE_PATH'))
	define('FEE_PATH', dirname(__FILE__));
if (!defined('APP_NAME'))
	define('APP_NAME', basename(dirname($_SERVER['SCRIPT_FILENAME'])));
if (!defined('APP_PATH'))
	define('APP_PATH', dirname($_SERVER['SCRIPT_FILENAME']));
if (!defined('DATA_PATH'))
	define('DATA_PATH', APP_PATH . '/data');

define('TPL_DIR', 'tpl'); //模板路径
//不带分组的应用路径
define('CACHE_PATH', DATA_PATH . '/cache'); //项目缓存目录
define('LOG_PATH', DATA_PATH . '/log'); //项目日志目录
define('TEMP_PATH', DATA_PATH . '/temp'); //项目临时数据目录
define('UPLOAD_PATH', DATA_PATH . '/upload'); //上传数据目录
define('HTML_PATH', APP_PATH . '/html'); // 项目html文件目录
define('PLUGIN_PATH', FEE_PATH . '/plugin'); //框架插件目录

//系统运行环境检测
define('IS_CGI', substr(PHP_SAPI, 0, 3) == 'cgi' ? 1 : 0);
define('IS_WIN', strstr(PHP_OS, 'WIN') ? 1 : 0);
// 当前文件名
if (!defined('_PHP_FILE_')) {
	if (IS_CGI) {
		//CGI/FASTCGI模式下
		$_temp = explode('.php', $_SERVER["PHP_SELF"]);
		define('_PHP_FILE_', rtrim(str_replace($_SERVER["HTTP_HOST"], '', $_temp[0] . '.php'), '/'));
	} else {
		define('_PHP_FILE_', rtrim($_SERVER["SCRIPT_NAME"], '/'));
	}
}

if (!defined('__ROOT__')) {
	// 网站URL根目录
	if (strtoupper(APP_NAME) == strtoupper(basename(dirname(_PHP_FILE_)))) {
		$_root = dirname(dirname(_PHP_FILE_));
	} else {
		$_root = dirname(_PHP_FILE_);
	}
	define('__ROOT__', (($_root == '/' || $_root == '\\') ? '' : $_root));
}

//生成核心缓存
$_run_file = DATA_PATH . '/run_tmp.php';
if (is_file($_run_file)) {
	require $_run_file;
} else {
	require FEE_PATH . "/conf/run.inc.php";
}

//加载别名
alias_import(array(
	'model' => FEE_PATH . '/core/model.class.php',
	'view_model' => FEE_PATH . '/core/model/view_model.class.php',
	'adv_model' => FEE_PATH . '/core/model/adv_model.class.php',
	'relation_model' => FEE_PATH . '/core/model/relation_model.class.php',
	'db' => FEE_PATH . '/classes/db/db.class.php',
	'template' => FEE_PATH . '/classes/template/template.class.php',
	'taglib' => FEE_PATH . '/classes/template/taglib.class.php',
	'taglib_cx' => FEE_PATH . '/classes/template/taglib/taglib_cx.class.php',
	'html_cache' => FEE_PATH . '/classes/html_cache.class.php',
	'cache' => FEE_PATH . '/classes/cache.class.php',
	'debug' => FEE_PATH . '/classes/debug.class.php',
	'cookie' => FEE_PATH . '/classes/cookie.class.php',
	'session' => FEE_PATH . '/classes/session.class.php',
	'behavior' => FEE_PATH . '/classes/behavior.class.php',
	'widget' => FEE_PATH . '/classes/widget.class.php',
));

//执行url控制器
dispatcher::dispatch();

//项目分组模板目录
define('TPL_PATH', APP_PATH . '/' . GROUP_NAME . '/' . TPL_DIR);
//主机协议
define('SITE_PROTOCOL', isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://');
//当前访问的主机名
define('SITE_URL', (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : ''));
//来源
define('HTTP_REFERER', isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '');
define('TIME', time());

// 记录加载文件时间
$GLOBALS['_loadTime'] = microtime(TRUE);
//版本信息
define('FEE_VERSION', '1.0');